<template>
  <!-- 包装配置表表单弹窗 -->
  <el-dialog
          v-model="visibleDialog"
          :title="props.title"
          width="70%"
          draggable
          @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
      <el-col :span="12">
      <el-form-item label="物料id" prop="materialId">
      <el-input v-if="!props.isReadonly" v-model="formData.materialId" placeholder="请输入物料id" clearable />
  
      <span v-else>{{formData.materialId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="物料编号" prop="materialCode">
      <el-input v-if="!props.isReadonly" v-model="formData.materialCode" placeholder="请输入物料编号" clearable />
  
      <span v-else>{{formData.materialCode}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="物料名称" prop="materialName">
      <el-input v-if="!props.isReadonly" v-model="formData.materialName" placeholder="请输入物料名称" clearable />
  
      <span v-else>{{formData.materialName}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="分类;占时没用" prop="classification">
      <el-input v-if="!props.isReadonly" v-model="formData.classification" placeholder="请输入分类;占时没用" clearable />
  
      <span v-else>{{formData.classification}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="采购周期" prop="purchasingCycle">
      <el-input v-if="!props.isReadonly" v-model="formData.purchasingCycle" placeholder="请输入采购周期" clearable />
  
      <span v-else>{{formData.purchasingCycle}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="最低库存" prop="minimumStock">
      <el-input v-if="!props.isReadonly" v-model="formData.minimumStock" placeholder="请输入最低库存" clearable />
  
      <span v-else>{{formData.minimumStock}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="最小采购量" prop="minimumPurchaseQuantity">
      <el-input v-if="!props.isReadonly" v-model="formData.minimumPurchaseQuantity" placeholder="请输入最小采购量" clearable />
  
      <span v-else>{{formData.minimumPurchaseQuantity}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建时间" prop="createTime">
      <el-input v-if="!props.isReadonly" v-model="formData.createTime" placeholder="请输入创建时间" clearable />
  
      <span v-else>{{formData.createTime}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建人" prop="createdUserId">
      <el-input v-if="!props.isReadonly" v-model="formData.createdUserId" placeholder="请输入创建人" clearable />
  
      <span v-else>{{formData.createdUserId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="创建人名" prop="createdUserName">
      <el-input v-if="!props.isReadonly" v-model="formData.createdUserName" placeholder="请输入创建人名" clearable />
  
      <span v-else>{{formData.createdUserName}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新时间" prop="lastUpdateTime">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdateTime" placeholder="请输入更新时间" clearable />
  
      <span v-else>{{formData.lastUpdateTime}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新人" prop="lastUpdatedUserId">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdatedUserId" placeholder="请输入更新人" clearable />
  
      <span v-else>{{formData.lastUpdatedUserId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="更新人名" prop="lastUpdatedUserName">
      <el-input v-if="!props.isReadonly" v-model="formData.lastUpdatedUserName" placeholder="请输入更新人名" clearable />
  
      <span v-else>{{formData.lastUpdatedUserName}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="是否启用" prop="enabled">
      <el-input v-if="!props.isReadonly" v-model="formData.enabled" placeholder="请输入是否启用" clearable />
  
      <span v-else>{{formData.enabled}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="删除标识" prop="deleted">
      <el-input v-if="!props.isReadonly" v-model="formData.deleted" placeholder="请输入删除标识" clearable />
  
      <span v-else>{{formData.deleted}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="备注" prop="remarks">
      <el-input v-if="!props.isReadonly" v-model="formData.remarks" placeholder="请输入备注" clearable />
  
      <span v-else>{{formData.remarks}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="数据版本号" prop="version">
      <el-input v-if="!props.isReadonly" v-model="formData.version" placeholder="请输入数据版本号" clearable />
  
      <span v-else>{{formData.version}}</span>
        </el-form-item>
      </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!props.isReadonly" type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import request from "@/utils/request";
  import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
  const dictionaryStore = useDictStoreHook();

  defineOptions({
    name: "MESMESPACKAGECONFIGADDORUPDATE",
    inheritAttrs: false,
  });

  const emit = defineEmits(["reset-table", "change-visible"]);

  const props = defineProps({
    visible: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    title: {
      type: String,
      default: () => {
        return "";
      },
    },
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
    isReadonly: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
  });

  const visibleDialog = ref(false);
  watch(
      () => props.visible,
      (newVal: boolean) => {
        visibleDialog.value = newVal;
        setTimeout(() => {
          formRef.value.clearValidate();
        }, 50);
      }
  );

  const rules = reactive({
    materialId: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    materialCode: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    materialName: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    classification: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    purchasingCycle: [
                        ],
    minimumStock: [
                        ],
    minimumPurchaseQuantity: [
                        ],
    createTime: [
                        ],
    createdUserId: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    createdUserName: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    lastUpdateTime: [
                        ],
    lastUpdatedUserId: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    lastUpdatedUserName: [
                        { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
                  ],
    enabled: [
                        ],
    deleted: [
                        ],
    remarks: [
                        { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" }
                  ],
    version: [
                        ],
  });

  const formRef = ref(ElForm);

  let formData = reactive({
 id: "",  materialId: "",  materialCode: "",  materialName: "",  classification: "",  purchasingCycle: "",  minimumStock: "",  minimumPurchaseQuantity: "",  createTime: "",  createdUserId: "",  createdUserName: "",  lastUpdateTime: "",  lastUpdatedUserId: "",  lastUpdatedUserName: "",  enabled: "",  deleted: "",  remarks: "",  version: "",   });

  watch(
          () => props.data,
          (newVal: any) => {
            formData.id = ""
            if (newVal?.id) {
              const {  id,  materialId,  materialCode,  materialName,  classification,  purchasingCycle,  minimumStock,  minimumPurchaseQuantity,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  enabled,  deleted,  remarks,  version,  } = newVal;
              Object.assign(formData, {  id,  materialId,  materialCode,  materialName,  classification,  purchasingCycle,  minimumStock,  minimumPurchaseQuantity,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  enabled,  deleted,  remarks,  version,  });
            }
          }
  );

  /** 关闭表单弹窗 */
  function closeDialog() {
    visibleDialog.value = false;
    emit("change-visible", false);
    resetForm();
  }

  /** 重置表单 */
  function resetForm() {
    formRef.value.resetFields();
    formRef.value.clearValidate();
    formData.id = ""
    formData.materialId = ""
    formData.materialCode = ""
    formData.materialName = ""
    formData.classification = ""
    formData.purchasingCycle = ""
    formData.minimumStock = ""
    formData.minimumPurchaseQuantity = ""
    formData.createTime = ""
    formData.createdUserId = ""
    formData.createdUserName = ""
    formData.lastUpdateTime = ""
    formData.lastUpdatedUserId = ""
    formData.lastUpdatedUserName = ""
    formData.enabled = ""
    formData.deleted = ""
    formData.remarks = ""
    formData.version = ""
  }

  const loading = ref(false);

  /** 包装配置表保存提交 */
  function handleSubmit() {
    formRef.value.validate(async (valid: any) => {
      if (valid) {
        loading.value = true;
        const text = formData.id ? "编辑" : "新增";
        await request({
          url: "/mes/mespackageconfig/save",
          method: "post",
          data: formData,
        }).finally(() => {
          loading.value = false;
        });
        ElMessage.success( text +`成功`);
        closeDialog();
        emit("reset-table", formData.id);
      }
    });
  }


  // 组件销毁时调用
  onBeforeUnmount(() => {
    visibleDialog.value = false;
    emit("change-visible", false);
  });


</script>
